package com.itheima.service;

import com.itheima.dao.RoleMapper;
import com.itheima.dao.StudentMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.Student;
import com.itheima.pojo.User;
import com.itheima.util.MD5Utils;
import com.itheima.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserService {
    public User login(User u) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // 对用户输入的密码使用MD5加密.对比加密后的密文
        String md5Password = MD5Utils.encode(u.getPassword());
        u.setPassword(md5Password);
        System.out.println("加密后的数据: "+u);
        User user = mapper.login(u);
        sqlSession.close();
        return user;
    }

    public PageBean<User> getUserByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        // 创建用于封装返回结果的对象
        PageBean<User> pb = new PageBean<User>();
        // 设置当前页页码
        pb.setCurrentPage(currentPage);
        // 设置每页显示的条数
        pb.setPageSize(pageSize);
        // 查询总页数,并封装到PageBean中
        int totalCount = mapper.queryTotalCount();
        pb.setTotalCount(totalCount);
        // 查询当前页的数据信息,并封装到Pb中
        //计算起始索引
        int startIndex = (currentPage-1)*pageSize;
        List<User> userList = mapper.getUserByPage(startIndex,pageSize);
        // 遍历用户对象,查询用户对应的角色信息
        for (User user : userList) {
            List<Role> list = roleMapper.findRoleByUserId(user.getId());
            user.setRoleList(list);
        }
        pb.setList(userList);
        // 关闭sessionSql
        sqlSession.close();
        return pb;
    }

    public void addUser(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // 向用户表中添加用户的基本信息
        mapper.addUser(user);
        // 添加用户角色信息
        int userId = user.getId();
        for (String roleId : user.getRoleIds()) {
            mapper.addUserAndRole(userId,roleId);
        }
        sqlSession.close();
    }

    public void updateUser(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        mapper.updateUser(user);
        //先删除角色信息
        roleMapper.delRoleById(user.getId());
        //再添加角色信息
        String[] roleIds = user.getRoleIds();
        for (String roleId : roleIds) {
            mapper.addUserAndRole(user.getId(),roleId);
        }

        sqlSession.close();
    }

    public void delUserById(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        roleMapper.delRoleById(id);

        mapper.delUserById(id);

        sqlSession.close();

    }
}
